package com.spsoft.vatoutinv.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.vatoutinv.model.dto.vmstransactionflowinvoice.*;
import com.spsoft.vatoutinv.model.vo.vmstransactionflowinvoice.VmsFlowInvoiceDetailVo;
import com.spsoft.vatoutinv.model.vo.vmstransactionflowinvoice.VmsManualInvoiceVo;
import com.spsoft.vatoutinv.model.vo.vmstransactionflowinvoice.VmsMergeQueryVo;
import com.spsoft.vatoutinv.model.vo.vmstransactionflowinvoice.VmsTransactionFlowInvoicePageVo;
import com.spsoft.vatoutinv.service.vmsnvattrandtadvaltax.VmsNvatTrandtadvaltaxsepService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author wangy
 * @date 2024/1/4 13:09
 */
@RestController
@Api(tags = {"交易流水开票"})
@RequestMapping("/transactionFlowInvoice")
@RequiredArgsConstructor
@Validated
@Slf4j
public class VmsTransactionFlowInvoiceController {

    public static final String LOG_MSG = "(交易流水开票控制层)";

    private final VmsNvatTrandtadvaltaxsepService transactionFlowInvoiceService;

    /**
     * 获取交易流水开票列表
     */
    @ApiOperation("交易流水开票-获取交易流水开票列表")
    @AnonymousAccess(value = "/getTransactionFlowInvoiceList", method = RequestMethod.POST, permissionCode = "VMS_FLOW_INVOICE")
    @Log(value = "交易流水开票-获取交易流水开票列表", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<IPage<VmsTransactionFlowInvoicePageVo>> getTransactionFlowInvoiceList(@RequestBody VmsTransactionFlowInvoicePageDto vmsTransactionFlowInvoicePageDto) {
        log.info("{}获取交易流水开票列表,入参:{}", LOG_MSG, JSONObject.toJSONString(vmsTransactionFlowInvoicePageDto));
        // 获票人名称  保单号/保全号/定期结算号 投保单号  交易日期字段 至少填充一项
        if (StringUtils.isBlank(vmsTransactionFlowInvoicePageDto.getKfhm())
                && StringUtils.isBlank(vmsTransactionFlowInvoicePageDto.getYwbh())
                && (StringUtils.isAnyEmpty(vmsTransactionFlowInvoicePageDto.getStartTime(), vmsTransactionFlowInvoicePageDto.getEndTime()))
                && StringUtil.isBlank(vmsTransactionFlowInvoicePageDto.getTbdh())) {
            return Result.ERROR(ResponseEnum.TRANSACTION_FLOW_INVOICE);
        }
        // 根据税率查询商品编码 获得商品名称
        return transactionFlowInvoiceService.getTransactionFlowInvoiceList(vmsTransactionFlowInvoicePageDto);
    }

    /**
     * 根据流水获取发票信息
     */
    @ApiOperation("交易流水开票-根据流水获取发票信息")
    @AnonymousAccess(value = "/getInvoiceInfoByTransactionFlow", method = RequestMethod.POST, permissionCode = "VMS_FLOW_INVOICE")
    @Log(value = "交易流水开票-根据流水获取发票信息", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<VmsManualInvoiceVo> getInvoiceInfoByTransactionFlow(@RequestBody List<Long> ids) {
        log.info("{}根据流水获取发票信息,流水号:{}", LOG_MSG, JSONObject.toJSONString(ids));
        return transactionFlowInvoiceService.getInvoiceInfoByTransactionFlow(ids);
    }

    /**
     * 流水开票
     */
    @ApiOperation("交易流水开票-流水开票(专票)")
    @AnonymousAccess(value = "/invoiceByTransactionFlow01", method = RequestMethod.POST, permissionCode = "VMS_FULL_FLOW_INVOICE_01")
    @Log(value = "交易流水开票-流水开票(专票)", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<Object> invoiceByTransactionFlow01(@RequestBody VmsTransactionFlowInvoiceDto dto) {
        log.info("{}流水开票(专票),入参:{}", LOG_MSG, JSONObject.toJSONString(dto));
        return transactionFlowInvoiceService.invoiceByTransactionFlow(dto);
    }

    /**
     * 流水开票
     */
    @ApiOperation("交易流水开票-流水开票(普票)")
    @AnonymousAccess(value = "/invoiceByTransactionFlow02", method = RequestMethod.POST, permissionCode = "VMS_FULL_FLOW_INVOICE_02")
    @Log(value = "交易流水开票-流水开票(普票)", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<Object> invoiceByTransactionFlow02(@RequestBody VmsTransactionFlowInvoiceDto dto) {
        log.info("{}流水开票(普票),入参:{}", LOG_MSG, JSONObject.toJSONString(dto));
        return transactionFlowInvoiceService.invoiceByTransactionFlow(dto);
    }

    /**
     * 流水拆分
     * VMS_SPLIT_FLOW_INVOICE
     */
    @ApiOperation("交易流水开票-流水拆分")
    @AnonymousAccess(value = "/splitTransactionFlow", method = RequestMethod.POST, permissionCode = "VMS_FULL_SPLIT")
    @Log(value = "交易流水开票-流水拆分", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<Object> splitTransactionFlow(@RequestBody VmsFlowSplitDto dto) {
        log.info("{}流水拆分,入参:{}", LOG_MSG, JSONObject.toJSONString(dto));
        return transactionFlowInvoiceService.splitTransactionFlow(dto);
    }

    /**
     * 流水合并查询
     * VMS_MERGE_FLOW_INVOICE
     */
    @ApiOperation("交易流水开票-流水合并查询")
    @AnonymousAccess(value = "/mergeQuery", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "交易流水开票-流水合并查询", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<IPage<VmsMergeQueryVo>> mergeQuery(@RequestBody VmsMergeDto dto) {
        log.info("{}流水合并查询,入参:{}", LOG_MSG, JSONObject.toJSONString(dto));
        return transactionFlowInvoiceService.mergeQuery(dto);
    }

    /**
     * 预合并
     * VMS_MERGE_FLOW_PREMERGE
     */

    @ApiOperation("交易流水开票-流水预合并")
    @AnonymousAccess(value = "/preMerge", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "交易流水开票-流水预合并", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<List<VmsMergeQueryVo>> preMerge(@RequestBody VmsMergeDto dto) {
        log.info("{}流水预合并,入参:{}", LOG_MSG, JSONObject.toJSONString(dto));
        return transactionFlowInvoiceService.preMerge(dto);
    }

    /**
     * 确定合并
     * VMS_MERGE_FLOW_MERGE
     */

    @ApiOperation("交易流水开票-确定合并")
    @AnonymousAccess(value = "/merge", method = RequestMethod.POST, permissionCode = "VMS_FULL_MERGE")
    @Log(value = "交易流水开票-确定合并", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<Object> merge(@RequestBody VmsMergeDto dto) {
        log.info("{}确定合并,入参:{}", LOG_MSG, JSONObject.toJSONString(dto));
        return transactionFlowInvoiceService.merge(dto);
    }

    /**
     * 查看合并/拆分主信息
     * VMS_MERGE_FLOW_MERGE_SPLIT_INFO
     */
    @ApiOperation("交易流水开票-查看合并/拆分主信息")
    @AnonymousAccess(value = "/getMergeSplitInfo", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "交易流水开票-查看合并/拆分信息主信息", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<VmsFlowInvoiceDetailVo> getMergeSplitInfo(@RequestParam("rdid") String id) {
        log.info("{}查看合并/拆分信息主信息,入参:{}", LOG_MSG, JSONObject.toJSONString(id));
        return transactionFlowInvoiceService.getMergeSplitInfo(id);
    }

    /**
     * 查看合并/拆分主信息
     * VMS_MERGE_FLOW_MERGE_SPLIT_ITEM
     */
    @ApiOperation("交易流水开票-查看合并/拆分明细信息")
    @AnonymousAccess(value = "/getMergeSplitItem", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "交易流水开票-查看合并/拆分明细信息", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<IPage<VmsFlowInvoiceDetailVo>> getMergeSplitItem(@RequestBody VmsFlowInvoiceDetailDto dto) {
        log.info("{}查看合并/拆分明细信息,入参:{}", LOG_MSG, JSONObject.toJSONString(dto));
        return transactionFlowInvoiceService.getMergeSplitItem(dto);
    }

    /**
     * 校验被拆分/合并流水是否已经开票
     */
    @ApiOperation("校验被拆分/合并流水是否已经开票")
    @AnonymousAccess(value = "/checkFlowInvoiceStatus", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "校验被拆分/合并流水是否已经开票", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<Object> checkFlowInvoiceStatus(@RequestParam("rdid") String id) {
        log.info("{}校验被拆分/合并流水是否已经开票,入参:{}", LOG_MSG, JSONObject.toJSONString(id));
        return transactionFlowInvoiceService.checkFlowInvoiceStatus(id);
    }

    /**
     * 还原流水
     */
    @ApiOperation("还原流水")
    @AnonymousAccess(value = "/restoreFlow", method = RequestMethod.POST, permissionCode = "VMS_FULL_RESTOREFLOW")
    @Log(value = "还原流水", type = LogActionType.SELECT, menuName = LogMenuEnum.TRANSACTION_FLOW_INVOICE)
    public Result<Object> restoreFlow(@RequestParam("rdid") String id) {
        log.info("{}还原流水,入参:{}", LOG_MSG, JSONObject.toJSONString(id));
        return transactionFlowInvoiceService.restoreFlow(id);
    }
}
